﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entidad;
using Datos.EntityFramework;

namespace Datos.DAO
{

    public class HabitacionDAO
    {
        BD_HOTEL_Entities bd = new BD_HOTEL_Entities();
        public List<SRH_HABITACION> listarhabitacion()
        {

            List<SRH_HABITACION> lista = new List<SRH_HABITACION>();
            try
            {
                var q = from c in bd.SRH_HABITACION
                        select c;
                lista = q.ToList();

            }
            catch (Exception)
            {
            }
            return lista;
        }



        public bool Insertar(SRH_HABITACION Obj)
        {
            try
            {
                bd.AddObject("SRH_HABITACION", Obj);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool Modificar(SRH_HABITACION obj)
        {
            try
            {
                bd.AttachTo("SRH_HABITACION", obj);
                bd.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                throw;
                            }
            return true;
        }

        public bool Eliminar(SRH_HABITACION obj)
        {
            try
            {
                bd.AttachTo("SRH_HABITACION", obj);
                bd.DeleteObject(obj);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
            return true;
        }

        public int ObtenerNrohabitacion()
        {
            int id_opcion = 0;
  
            try
            {
                var q = (from c in bd.SRH_HABITACION
                         select c.idHabitacion).DefaultIfEmpty().Max();

                if (q == 0)
                {
                    id_opcion = 100;
                }
                else
                {
                    id_opcion = Convert.ToInt32(q) + 1;
                }
             
            }
            catch (Exception ex)
            {
                throw;
            }

            return id_opcion;
        }
     
        public static string Right(string param, int length)
        {

            int value = param.Length - length;
            string result = param.Substring(value, length);
            return result;
        }

        public IEnumerable<Object> listarhabitacionesDisponobles()
        {
            IEnumerable<Object> lista = new List<Object>();
            try
            {
                var q = from ha in bd.SRH_HABITACION
                        from cat in bd.SRH_CATEGORIA
                        from tip in bd.SRH_TIPOHABITACION
                        where ha.idCategoria == cat.idCategoria &&
                        ha.idtipoHabitacion == tip.idtipoHabitacion &&
                        ha.estado==0
                        select new
                        {
                           estadoHabitacion= ha.estado==0?"DISPONIBLE":"OCUPADO",
                           estado=ha.estado,
                           ha.idHabitacion,
                           ha.piso,
                           ha.precioBase,
                           ha.idCategoria,
                           ha.idtipoHabitacion,
                           categoria=cat.nombre,
                           tipohabitacion=tip.nombre,
                           tip.detalle
                        };

                lista = q.ToList();
            }
            catch (Exception)
            {
                
                throw;
            }
            return lista;
        }
    }
}
